package me.winds.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import java.text.SimpleDateFormat;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

  public Toast toast;
  BroadcastReceiver receiver;
  public final static String CHANNEL_METHOD = "me.winds.flutter/method";
  public final static String CHANNEL_EVENT = "me.winds.flutter/event";
  public final static String METHOD_SHOW_TOAST = "showToast";
  public final static String METHOD_GET_SYSTEM_TIME = "getSystemTime";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);

    initMethodChannel();
    initEventChannel();
  }

  private void initEventChannel() {
    new EventChannel(getFlutterView(), CHANNEL_EVENT).setStreamHandler(new EventChannel.StreamHandler() {
      @Override
      public void onListen(Object o, EventChannel.EventSink eventSink) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        receiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
            boolean connected = isNetworkConnected(context);
            eventSink.success(connected ? "网络可用" : "网络不可用");
          }
        };
        registerReceiver(receiver, filter);
      }

      @Override
      public void onCancel(Object o) { //取消
        if (receiver != null) {
          unregisterReceiver(receiver);
        }
      }
    });
  }

  private void initMethodChannel() {
    new MethodChannel(getFlutterView(), CHANNEL_METHOD).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
      @Override
      public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        switch (methodCall.method) {
          case METHOD_SHOW_TOAST:
            showToast(methodCall.hasArgument("msg") ? methodCall.argument("msg").toString() : ""); //无返回值
            break;
          case METHOD_GET_SYSTEM_TIME:
            result.success(getSystemTime(methodCall.argument("pattern"))); //有返回值
            break;
          default:
            showToast("调用方法未定义: " + methodCall.method);
            break;

        }
      }
    });
  }


  public static boolean isNetworkConnected(Context context) {
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
    return mNetworkInfo != null && mNetworkInfo.isConnected();
  }

  private String getSystemTime(String pattern) {
    return new SimpleDateFormat(TextUtils.isEmpty(pattern) ? "yyyy-MM-dd hh:mm:ss" : pattern).format(System.currentTimeMillis());
  }

  public void showToast(String msg) {
    toast.setText(msg);
    toast.show();
  }
}

